<?php
namespace app\api\model;

use think\Db;
use think\Model;

class MasterModel extends Model
{
        function masterlist() {
        $masters =  Db::name('master')->select();
        return  $masters;
	    }
	    function masterdetail($masterid) {
        $result = Db::name('master')->where('masterid',$masterid)->find();
        return $result;
    }

    function collect($userid, $masterid) {
        //判断用户存在
        $hasUser = Db::name('user')
            ->where('userid', $userid)
            ->select();

        if (!$hasUser) {
            return ['success' => -1, 'msg' => '无此用户'];
        }

        //判断大师
        $hasMaster = Db::name('master')
            ->where('masterid', $masterid)
            ->select();

        if (!$hasMaster) {
            return ['success' => -1, 'msg' => '无此大师'];
        }

        //是否已经收藏
        $isCollect = db('master_follow')
            ->where('masterid', $masterid)
            ->where('userid', $userid)
            ->find();

        if ($isCollect) {
            $res = db('master_follow')
                ->where('masterid', $masterid)
                ->where('userid', $userid)
                ->delete();
            if ($res) {
                return ['success' => 0, 'msg'=>'取消收藏'];
            } else {
                return ['success' => -1, 'msg'=>'取消收藏失败'];
            }
        } else {
            $data = ['userid' => $userid, 'masterid' => $masterid];
            $res = db('master_follow')->insert($data);
            if ($res) {
                return ['success' => 1, 'msg'=>'收藏成功'];
            } else {
                return ['success' => -1,'msg'=> '收藏失败'];
            }
        }
    }

    function isCollect($userid, $masterid) {
        $res = db('master_follow')
            ->where('masterid', $masterid)
            ->where('userid', $userid)
            ->find();
        if ($res) {
            return ['success' => 1];
        } else {
            return ['success' => 0];
        }
    }

    function getReply($masterid) {
        $res = db('master_reply')
            ->where('masterid', $masterid)
            ->select();

        return $res;
    }

    function reply($userid, $masterid,$comment) {
        $data = ['userid'=>$userid,'masterid'=>$masterid,'comment'=>$comment];
        $res = db('master_reply')
            ->insert($data);
        return ['success' => $res];
    }

    function getlesson($masterid) {
           $res = db('master_lesson')->select();
           return $res;
    }

    function collectMasterList($userid){
        $res = Db::table('master_follow')
            ->alias('follow')
            ->join('master',"follow.masterid=master.masterid and follow.userid = '$userid'")
            ->select();
        return $res;
    }
}

//登陆 活动列表 活动详情 活动咨询 俱乐部列表 大师列表 大师详情 比赛列表 比赛详情